import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/common/charts_pie.dart';
import 'package:enterprise_v6_app/app/data/entities/inspec/inspec_plan_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/inspec/inspec_task_entity.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:syncfusion_flutter_charts/charts.dart';

class InspecAllViewController extends GetxController {
  /// 巡检计划总数
  int planTotal = 0;

  /// 巡检计划
  List<ChartData> questionData = [
    ChartData('已完成', 0, const Color(0xFF33A9FF)),
    ChartData('进行中', 0, const Color(0xFF5C6DFF)),
    ChartData('漏检  ', 0, const Color(0xFFFD6B6B)),
  ];

  var initRiskData = <ChartData>[
    ChartData('四级报警', 1, Colors.yellow),
    ChartData('三级报警', 1, Colors.red),
    ChartData('二级报警', 1, Colors.blue),
    ChartData('一级报警', 1, const Color(0xffFF5C6D)),
  ];

  List<String> tableTitle = ['未处理', '消警', '超时', '误报'];

  List<String> test = ['巡检点', '报警', '消警', '违规率'];

  /// 颜色组
  List<Color> colorList = [
    const Color(0xff649FFF),
    const Color(0xffFFCE24),
    const Color(0xffFD6B6B),
    const Color(0xFF5C6DFF),
    Colors.green,
    Colors.deepPurpleAccent,
    Colors.pinkAccent,
    Colors.blueAccent,
    Colors.yellowAccent,
    Colors.purple,
    Colors.orange,
    Colors.tealAccent
  ];

  /// 折线图数据
  List<SplineSeries<PieData, String>> lineData = [];

  @override
  void onReady() {
    super.onReady();
    getPlanTotal();
    getTaskTotal();
    getDepartmentTotal();
  }

  // 查询巡检计划接口
  void getPlanTotal() async {
    final res = await HttpUtil()
        .generateGetRequest<InspecPlanEntity>(
          Interface.getInspectionPlan,
        )
        .execute();
    res.onSuccess((result) {
      if (result.data!.planTotalNum != 0) {
        planTotal = result.data!.planTotalNum!;
      } else {
        planTotal = result.data!.completedQuantity! + result.data!.quantityInProgress! + result.data!.numberOfMissedDetections!;
      }
      questionData[0].y = result.data!.completedQuantity!;
      questionData[1].y = result.data!.quantityInProgress!;
      questionData[2].y = result.data!.numberOfMissedDetections!;
      update(['plan']);
    });
  }

  // 巡检任务接口数据
  InspecTaskEntity inspecTaskEntity = InspecTaskEntity();

  // 查询巡检任务接口
  void getTaskTotal() async {
    final res = await HttpUtil().generateGetRequest<InspecTaskEntity>(Interface.getInspectionTask).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        inspecTaskEntity = result.data!;
        if (inspecTaskEntity.taskTotalNum == 0) {
          inspecTaskEntity.taskTotalNum =
              inspecTaskEntity.toBeCompleted! + inspecTaskEntity.quantityCompleted! + inspecTaskEntity.missingNum! + inspecTaskEntity.reExaminationNum!;
        }
      }
      update(['task']);
    });
  }

  // 查询巡检部门接口
  void getDepartmentTotal() async {
    final res = await HttpUtil().generateGetRequest<List<InspecTaskEntity>>(Interface.getDepartmentInspection).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        final List<InspecTaskEntity> list = result.data!;

        final List<List<PieData>> all = [];

        /// 计划数[planNum]
        final List<PieData> piPlanNum = [];

        /// 已完成[quantityCompleted]
        final List<PieData> piQuantityCompleted = [];

        /// 带巡检[toBeCompleted]
        final List<PieData> piToBeCompleted = [];

        /// 补检 [reExaminationNum]
        final List<PieData> piReExaminationNum = [];

        /// 漏检[missingNum]
        final List<PieData> piMissingNum = [];

        final List<String> name = [
          '计划',
          '已完成',
          '待巡检',
          '逾期补检',
          '漏检',
        ];

        all.add(piPlanNum);
        all.add(piQuantityCompleted);
        all.add(piToBeCompleted);
        all.add(piReExaminationNum);
        all.add(piMissingNum);


        for (int i = 0; i < list.length; i++) {
          piPlanNum.add(PieData('${list[i].departmentName}', list[i].planNum!));
          piQuantityCompleted.add(PieData('${list[i].departmentName}', list[i].quantityCompleted!));
          piToBeCompleted.add(PieData('${list[i].departmentName}', list[i].toBeCompleted!));
          piReExaminationNum.add(PieData('${list[i].departmentName}', list[i].reExaminationNum!));
          piMissingNum.add(PieData('${list[i].departmentName}', list[i].missingNum!));
        }

        for (int f = 0; f < 5; f++) {
          lineData.add(SplineSeries<PieData, String>(
            dataSource: all[f],
            xValueMapper: (PieData sales, _) => sales.xData,
            yValueMapper: (PieData sales, _) => sales.yData,
            name: name[f],
          ));
        }

      }
      update(['department']);
    });
  }

  // 查询报警接口
  void getAlarmTotal() async {
    final res = await HttpUtil().generateGetRequest('').execute();
    res.onSuccess((result) {
      update(['alarm']);
    });
  }
}
